﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Informaiton_Retrieval_Hw3.UI
{
    public partial class MainForm : Form
    {
        Model _model;
        bool hasFile;
        public MainForm()
        {
            InitializeComponent();
            _model = new Model(this);
            hasFile = false;
        }

        private void openDocs()
        {
            _model = new Model(this);
            Stream myStream;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "sgm files (*.sgm)|*.sgm";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.Multiselect = true;
            LoadFileForm form = new LoadFileForm();
            hasFile = false;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    form.Show();
                    for (int i = 0; i < openFileDialog1.FileNames.Length; i++)
                    {
                        hasFile = true;
                        form.Progress = getProgerss(openFileDialog1.FileNames.Length, i);
                        string strFilename = openFileDialog1.FileNames[i];
                        _model.load(strFilename);
                    }
                    myStream.Close();
                }
            }

            form.Close();
            if (hasFile)
                _model.InitData();


        }

        private int getProgerss(float total, float count)
        {
            float r = (count + 1) / total * 100f;
            return (int)r;
        }

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
            openDocs();
            DataGridViewRowCollection rows = this.dataGridView1.Rows;
            rows.Clear();
            Dictionary<Category, int[]> docDictionary = _model.docDictionary;

            foreach (Category c in docDictionary.Keys)
            {
                rows.Add(new Object[] { c.ToString(), docDictionary[c].Length });
            }
            if (hasFile)
                rows.Add(new Object[] { "Total", _model.TrainDocs });
        }

        private void submitbutton_Click(object sender, EventArgs e)
        {
            string[] data = new string[] { textBox1.Text };

            classLabel.Text = "Answer is " + _model.ClassifyTestData(data).ToString() + " !";
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || hasFile == false) submitbutton.Enabled = false;
            else submitbutton.Enabled = true;
        }
    }
}
